package br.com.prime.model.bussines;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.prime.model.ConnectionFactory;
import br.com.prime.model.pojo.Materia;

public class MateriaDAO {

	private Connection connection;

	public MateriaDAO() {
		try {
			this.connection = new ConnectionFactory().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Materia> getAll() {
		List<Materia> materias = new ArrayList<Materia>();
		String sql = "SELECT * FROM MATERIA";
		try {
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			materias = montarObjeto(rs);
			rs.close();
			stmt.close();
			return materias;
		} catch (SQLException e) {
			e.printStackTrace();
			return materias;
		}
	}

	public List<Materia> montarObjeto(ResultSet rs) {
		List<Materia> materias = new ArrayList<Materia>();
		try {
			while (rs.next()) {
				Materia materia = new Materia();
				materia.setID_MATERIA(rs.getInt("iD_MATERIA"));
				materia.setNOME(rs.getString("NOME"));
					
				materias.add(materia);
			}
			return materias;
		} catch (Exception e) {
			e.printStackTrace();
			return materias;
		}
	}
}